	package elements.axis ;
	import flash.display.Sprite;
	import flash.geom.Point;
	import ColorConverter.Color;
	import string.Utils;
	
	typedef RADARAXISSTRUCT = {
		var stroke		: Int;
		var color		: Color;
		var grid_color		: Color;
		var min			: Int;
		var max			: Int;
		var steps		: Int;
	};
	
	class RadarAxis extends Sprite {
		
		 var style:RADARAXISSTRUCT;
		 var TO_RADIANS: Float; // = Math.PI / 180;
		
		 var color: Float;
		 var grid_color: Float;
		 var labels:RadarAxisLabels;
		 var spoke_labels:RadarSpokeLabels;
		
		public function new( json:Dynamic )
		{
			super();

			TO_RADIANS = Math.PI / 180;

			// default values
			this.style = {
				stroke:			2,
				color:			Hex('#784016'),
				grid_color:		Hex('#F5E1AA'),
				min:			0,
				max:			null,
				steps:			1
			};
			
			if( json != null )
				ObjectHelper.merge_2( json, this.style );

			//this.color = Utils.get_color( this.style.color );
			//this.grid_color = Utils.get_color( this.style['grid-color'] );
				
			this.color = ColorConverter.toFloat(style.color); 
			this.grid_color = ColorConverter.toFloat( this.style.grid_color );
			
			this.labels = new RadarAxisLabels( json.labels );
			this.addChild( this.labels );
			
			this.spoke_labels = new RadarSpokeLabels( Reflect.field(json,'spoke-labels') );
			this.addChild( this.spoke_labels );
		}
		
		//
		// how many items in the X axis?
		//
		public function get_range():Range {
			return new Range( this.style.min, this.style.max, this.style.steps, false );
		}
		
		public function resize( sc:ScreenCoordsRadar ):Void
		{
			this.x = 0;
			this.y = 0;
			this.graphics.clear();
			
			// this is going to change the radius
			this.spoke_labels.resize( sc );
			
			var count: Int = sc.get_angles();
			
			// draw the grid behind the axis
			this.draw_grid( sc, count );
			this.draw_axis( sc, count );
			
			this.labels.resize( sc );
		}
		
		 function draw_axis( sc:ScreenCoordsRadar, count: Int ): Void {
			
			this.graphics.lineStyle(this.style.stroke, this.color, 1, true);
			
			for ( i in 0...count ) {

				//
				// assume 0 is MIN
				//
				var p:flash.geom.Point = sc.get_get_x_from_pos_and_y_from_val( i, 0 );
				this.graphics.moveTo( p.x, p.y );
				
				var q:flash.geom.Point = sc.get_get_x_from_pos_and_y_from_val( i, sc.get_max() );
				this.graphics.lineTo( q.x, q.y );
			}
		}
		
		 function draw_grid( sc:ScreenCoordsRadar, count: Int ):Void {
		
			this.graphics.lineStyle(1, this.grid_color, 1, true);
			
			// floating point addition error:
			var max: Float = sc.get_max() + 0.00001;
			
			var r_step: Float = this.style.steps;
			var p:flash.geom.Point;
			
			//
			// start in the middle and move out drawing the grid,
			// don't draw at 0
			//
			/*
			for ( var r_pos: Float = r_step; r_pos <= max; r_pos+=r_step ) {
				
				p = sc.get_get_x_from_pos_and_y_from_val( 0, r_pos );
				this.graphics.moveTo( p.x, p.y );
				
				// draw from each spoke
				for ( i in 1...(count+1) ) {
					
					p = sc.get_get_x_from_pos_and_y_from_val( i, r_pos );
					this.graphics.lineTo( p.x, p.y );
				}
			}
			*/
			var _count : Int = count + 1;

			var r_pos : Float = r_step;
			while (r_pos <= max)
			{
				p = sc.get_get_x_from_pos_and_y_from_val( 0, r_pos );
				this.graphics.moveTo( p.x, p.y );
				
				// draw from each spoke
				for ( i in 1..._count ) {	
					p = sc.get_get_x_from_pos_and_y_from_val( i, r_pos );
					this.graphics.lineTo( p.x, p.y );
				}
				r_pos+=r_step;
			}
		}
		
		public function die(): Void {
			
			this.style = null;
			this.labels.die();
			this.spoke_labels.die();
		
			this.graphics.clear();
			while ( this.numChildren > 0 )
				this.removeChildAt(0);
		}
	}
